package com.wwj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wwj.common.constant.MessageConstant;
import com.wwj.common.constant.StatusConstant;
import com.wwj.common.result.PageResult;
import com.wwj.mapper.CheckMapper;
import com.wwj.mapper.OrderMapper;
import com.wwj.mapper.SetmealCheckMapper;
import com.wwj.mapper.SetmealMapper;
import com.wwj.pojo.dto.SetmealDTO;
import com.wwj.pojo.dto.SetmealPageQueryDTO;
import com.wwj.pojo.entity.Check;
import com.wwj.pojo.entity.Setmeal;
import com.wwj.pojo.entity.SetmealCheck;
import com.wwj.pojo.vo.SetmealVO;
import com.wwj.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealCheckMapper setmealCheckMapper;

    @Resource
    private CheckMapper checkMapper;


    //新增套餐
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insertSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<Long> checkIds = setmealDTO.getChecksIds();
        log.info("CheckIds{}",checkIds);
        log.info("setmealId{}",setmealId);
        List<Check> checks=checkMapper.selectByIds(checkIds);
        List<SetmealCheck> setmealChecks = new ArrayList<>();
        for (Check check : checks) {
            SetmealCheck setmealCheck =SetmealCheck.builder()
                    .checkId(check.getId())
                    .price(check.getPrice())
                    .name(check.getName())
                    .setmealId(setmealId)
                    .description(check.getDescription())
                    .build();
            setmealChecks.add(setmealCheck);
        }
        log.info("checks{}",checks);
        log.info("setmealChecks{}",setmealChecks);
        setmealCheckMapper.insertSetmealCheckBatch(setmealChecks);

    }

    //删除套餐
    @Override
    public void deleteByIds(Long id) {

        Setmeal setmeal=setmealMapper.selectById(id);
        if(setmeal.getStatus() == StatusConstant.ENABLE){
            throw new RuntimeException(MessageConstant.STATUS_1);
        }
        setmealMapper.deleteById(id);
        setmealCheckMapper.deleteBySetmealId(id);
    }

    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        log.info("setmeal{}",setmeal);
        setmealMapper.update(setmeal);
        setmealCheckMapper.deleteBySetmealId(setmealDTO.getId());
        List<Long> checkIds = setmealDTO.getChecksIds();
        log.info("CheckIds{}",checkIds);
        List<Check> checks=checkMapper.selectByIds(checkIds);
        List<SetmealCheck> setmealChecks = new ArrayList<>();
        for (Check check : checks) {
            SetmealCheck setmealCheck =SetmealCheck.builder()
                    .checkId(check.getId())
                    .price(check.getPrice())
                    .name(check.getName())
                    .setmealId(setmealDTO.getId())
                    .description(check.getDescription())
                    .build();
            setmealChecks.add(setmealCheck);
        }
        setmealCheckMapper.insertSetmealCheckBatch(setmealChecks);
    }

    //管理端分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        for (SetmealVO vo : result) {
            List<Long> checksIds=setmealCheckMapper.getCheckIdsBySetmealId(vo.getId());
            List<SetmealCheck> setmealChecks=setmealCheckMapper.getSetmealCheckBySetmealId(vo.getId());
            vo.setSetmealChecks(setmealChecks);
            vo.setChecksIds(checksIds);
        }
        return new PageResult(total,result);
    }
    //用户端分页查询套餐
    @Override
    public PageResult pageQueryUser(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //用户端只查询启用的套餐

        setmealPageQueryDTO.setStatus(StatusConstant.ENABLE);
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        for (SetmealVO vo : result) {
            List<Long> checksIds=setmealCheckMapper.getCheckIdsBySetmealId(vo.getId());
            List<SetmealCheck> setmealChecks=setmealCheckMapper.getSetmealCheckBySetmealId(vo.getId());
            vo.setSetmealChecks(setmealChecks);
            vo.setChecksIds(checksIds);
        }
        return new PageResult(total,result);
    }

    //套餐起售、停售
    @Override
    public void status(Integer status, Long id) {

        Setmeal setmeal=Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);

    }

    //根据套餐id查询检查项目
    @Override
    public List<Check> selectCheckBySetmealId(Long id) {

        return setmealCheckMapper.getCheckBySetmealId(id);
    }









}
